package gases.as3.librarys.components.ui.container
{
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import gases.as3.librarys.components.skin.data.BaseSkin;
	import gases.as3.librarys.components.skin.data.PanelSkin;
	import gases.as3.librarys.components.ui.GSkinBox;
	import gases.as3.librarys.components.ui.GUIComponents;
	
	[Event(name="panelClick",  	type="flash.events.Event")]
	public class GPanel extends GUIComponents
	{
		public function GPanel(w:Number,h:Number)
		{
			_width = 		w;
			_height =	    h;
			super();
		}
		
		override protected function initialization():void
		{
			super.initialization();
			this.mouseEnabled = true;
			_background = new GSkinBox();
			
			this.addChild(_background);
			moveState();
		}
		
		override public function set skin(value:BaseSkin):void
		{
			_panelSkin = value as PanelSkin;
			super.skin = value;
			_background.source = _panelSkin.background;
			setWH();
		}
		
		override public function dispose():void
		{
			removeMouseListenrs();
			this.removeChild(_background);
			_background.dispose();
			_background = null;
			super.dispose();
		}
		
		public function setMoveState(bool:Boolean):void
		{
			_canMove = bool;
			moveState();
		}
		
		private function setWH():void
		{
			_background.setHeight(_height);
			_background.setWidth(_width);
		}
		
		private function moveState():void
		{
			if(_canMove)
			{
				addMouseListeners();
			}
			else
			{
				removeMouseListenrs();
			}
		}
		
		private function addMouseListeners():void
		{
			this.addEventListener(MouseEvent.MOUSE_DOWN,onMouseDownHandler);
			this.addEventListener(MouseEvent.MOUSE_UP,onMouseUpHandler);
			this.addEventListener(MouseEvent.CLICK,onClickHandler);
		}
		
		private function removeMouseListenrs():void
		{
			this.removeEventListener(MouseEvent.MOUSE_DOWN,onMouseDownHandler);
			this.removeEventListener(MouseEvent.MOUSE_UP,onMouseUpHandler);
			this.removeEventListener(MouseEvent.MOUSE_DOWN,onClickHandler);
		}
		
		/**
		 * 移动ui 
		 * @param e
		 * 
		 */		
		private function onMouseDownHandler(e:MouseEvent):void
		{
			this.startDrag();
		}
		
		/**
		 * 停止移动ui 
		 * @param e
		 * 
		 */		
		private function onMouseUpHandler(e:MouseEvent):void
		{
			this.stopDrag();
		}
		
		/**
		 * 点击panel并发送事件，用来展示在最前面 
		 * @param e
		 * 
		 */		
		private function onClickHandler(e:MouseEvent):void
		{
			this.dispatchEvent(new Event("panelClick"));
		}
		
		public function get vAlign():String
		{
			return _vAlign;
		}
		
		public function set vAlign(value:String):void
		{
			_vAlign = value;
		}
		
		public function get hAlign():String
		{
			return _hAlign;
		}
		
		public function set hAlign(value:String):void
		{
			_hAlign = value;
		}
		
		private var _vAlign:String;
		private var _hAlign:String;
		
		private var _width:Number;
		private var _height:Number;
		private var _background:GSkinBox;
		
		private var _panelSkin:PanelSkin;
		
		private var _canMove:Boolean = true;
	}
}


